//
//  LLT_CollectViewController_LXM.m
//  CoderLLT
//
//  Created by lanou3g on 15/12/25.
//  Copyright © 2015年 刘晓萌. All rights reserved.
//

#import "LLT_CollectViewController_LXM.h"
#import "LLT_NewsTableViewCell_LXH.h"
#import "LLT_VideoDetailTableViewCell_LXH.h"
#import "LLT_VideoModel.h"
#import "LLT_NewsModel.h"
#import "LLT_NewsDetailViewController_LXH.h"
#import <AVOSCloud/AVOSCloud.h>
#import "LLT_PlayViewController_LXM.h"
#import "DirectionMenubarView.h"
#import "UIViewController+LLT_LoginAndRegist_LXM.h"
@interface LLT_CollectViewController_LXM ()<UIScrollViewDelegate,UITableViewDataSource,UITableViewDelegate>
//新闻tableView
@property (weak, nonatomic) IBOutlet UITableView *CollectNewsTableView;
//解说tableView
@property (weak, nonatomic) IBOutlet UITableView *CollectPersonTableView;
//新闻数组
@property (nonatomic, strong) NSMutableArray *newsArray;
//视频数组
@property (nonatomic, strong) NSMutableArray *videoArray;
//主体的scrollView
@property (weak, nonatomic) IBOutlet UIScrollView *mainScrollView;
//标题View
@property (weak, nonatomic) IBOutlet UIView *titleView;
//titleView的主体插件
@property (strong, nonatomic) DirectionMenubarView *dicmVC;
//滑动位置标记
@property (assign, nonatomic) NSInteger indexScrollFlag;
@end

@implementation LLT_CollectViewController_LXM


#pragma mark --- 懒加载

/**
 * 新闻数组
 */
-(NSMutableArray *)newsArray{
    if (!_newsArray)
    {
        
        _newsArray = [NSMutableArray new];
        
    }
    
    return _newsArray;
    
}

/**
 * 视频数组
 */
-(NSMutableArray *)videoArray
{
    if (!_videoArray)
    {
        
        _videoArray = [NSMutableArray new];
        
    }
    
    return _videoArray;
    
}

#pragma mark --- 设置界面

- (void)viewDidLoad {
   
    
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self makeView];
    [self makeTitleView];
    [self makeTableView];
}

-(void)makeView{
    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"bgBlack"] forBarMetrics:UIBarMetricsDefault];
    self.navigationItem.title = @"收藏";
    //设置中间titile的字体颜色
    [[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
    UIBarButtonItem *left = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-yinlefanhui"] style:UIBarButtonItemStyleDone target:self action:@selector(leftAction:)];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = left;
    self.mainScrollView.delegate =self;
}

-(void)makeTitleView{
    self.dicmVC = [DirectionMenubarView instntceWithFrame:CGRectMake(0, 0, WIDTH,32) andTitleArr:(@[@"新闻",@"视频"].mutableCopy)];
    [self.titleView addSubview:self.dicmVC];
    __weak typeof(self) __weakSelf = self;
    self.dicmVC.alpha  = 0.9;
    
    self.dicmVC.doChang = ^{
        __weakSelf.mainScrollView.contentOffset = CGPointMake([__weakSelf.dicmVC getTapedIndex]*WIDTH, __weakSelf.mainScrollView.contentOffset.y);
    };

}
-(void)leftAction:(id )sender{
    DissMissBack;
}
-(void)viewWillAppear:(BOOL)animated
{
    [self makeNewsData];
    
    [self makeVideoData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark --- 代理方法
#pragma mark --- scrollView代理方法和 滑动点击
/*
 * scrollView滑动调用方法
 */
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if(scrollView.tag == 388){
        self.indexScrollFlag = self.mainScrollView.contentOffset.x/self.view.frame.size.width;
        [self.dicmVC changTitleWithIndex:self.indexScrollFlag];
    }
}

#pragma mark --- 页面设置

/**
 * 获取新闻数据
 */
-(void)makeNewsData
{
    AVQuery *query = [AVQuery queryWithClassName:@"LLT_NewsModel"];
    [query whereKey:@"cuserName" equalTo:[AVUser currentUser].mobilePhoneNumber];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        self.newsArray = [NSMutableArray new];
        if (!error) {
            for (int i = 0; i < objects.count;  i++) {
                NSDictionary *dic = objects[i][@"localData"];
                LLT_NewsModel *model = [[LLT_NewsModel alloc]init];
                [model setValuesForKeysWithDictionary:dic];
                [self.newsArray addObject:model];
            }
            
            [self.CollectNewsTableView reloadData];
        } else {
            // 输出错误信息
            //NSLog(@"Error: %@ %@", error, [error userInfo]);
        }
    }];
    
}
/**
 * 获取视频数据
 */
-(void)makeVideoData
{
    AVQuery *query = [AVQuery queryWithClassName:@"LLT_VideoModel"];
    [query whereKey:@"cuserName" equalTo:[AVUser currentUser].mobilePhoneNumber];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        self.videoArray = [NSMutableArray new];
        if (!error) {
            for (int i = 0; i < objects.count;  i++) {
                NSDictionary *dic = objects[i][@"localData"];
                LLT_VideoModel *model = [[LLT_VideoModel alloc]init];
                [model setValuesForKeysWithDictionary:dic];
                [self.videoArray addObject:model];
            }
            
            [self.CollectPersonTableView reloadData];
        } else {
            // 输出错误信息
            //NSLog(@"Error: %@ %@", error, [error userInfo]);
        }
    }];

}



/**
 * 设置tableView代理  注册
 */
-(void)makeTableView
{
    self.CollectNewsTableView.delegate = self;
    self.CollectNewsTableView.dataSource = self;
    self.CollectNewsTableView.tag = 666;
    self.CollectPersonTableView.delegate = self;
    self.CollectPersonTableView.dataSource = self;
    self.CollectPersonTableView.tag = 667;
    [self.CollectNewsTableView registerNib:[UINib nibWithNibName:@"LLT_NewsTableViewCell_LXH" bundle:nil] forCellReuseIdentifier:@"News_cell"];
    
    [self.CollectPersonTableView registerNib:[UINib nibWithNibName:@"LLT_VideoDetailTableViewCell_LXH" bundle:nil] forCellReuseIdentifier:@"SmallCell"];
}

#pragma mark --- 设置tableView
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView.tag == 666)
    {
        if (!self.newsArray.count) {
            return 1;
        }
        return self.newsArray.count;
    }
    else if(tableView.tag == 667)
    {
        if (!self.videoArray.count) {
            return 1;
        }
        return self.videoArray.count;
    }
    else
    {
        return 0;
    }
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    
    if (tableView.tag == 666)
    {
        if (self.newsArray.count == 0) {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"zhanwei"];
            if(!cell){
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"zhanwei"];
            }
            cell.textLabel.text = @"暂无收藏";
            cell.userInteractionEnabled = NO;
            return cell;
        }else{
            LLT_NewsTableViewCell_LXH *cell = [tableView dequeueReusableCellWithIdentifier:@"News_cell" forIndexPath:indexPath];
            LLT_NewsModel *news = self.newsArray[indexPath.row];
            cell.newsModel = news;
            return cell;
        }
    }
    else if(tableView.tag == 667)
    {
        if (self.videoArray.count == 0) {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"zhanwei"];
            if(!cell){
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"zhanwei"];
            }
            cell.textLabel.text = @"暂无收藏";
            cell.userInteractionEnabled = NO;
            return cell;
        }else{
            LLT_VideoDetailTableViewCell_LXH *cell = [tableView dequeueReusableCellWithIdentifier:@"SmallCell" forIndexPath:indexPath];
            LLT_VideoModel *video = self.videoArray[indexPath.row];
            cell.videoModel = video;
             return cell;
        }
    }else{
        return nil;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag == 666) {
        
        LLT_NewsModel *news = self.newsArray[indexPath.row];
        
        LLT_NewsDetailViewController_LXH *newsDetailViewController = [MainStoryBoard instantiateViewControllerWithIdentifier:@"newsDetail"];
        
        newsDetailViewController.urlString = news.newsUrl;
        
        newsDetailViewController.newsModel = news;
        
        [self presentViewController:newsDetailViewController animated:YES completion:nil];
        
    }else if (tableView.tag == 667){
        
        LLT_PlayViewController_LXM *playViewController = [MainStoryBoard instantiateViewControllerWithIdentifier:@"playVC"];
        
        LLT_VideoModel *videoModel = self.videoArray[indexPath.row];
        playViewController.videoModel = videoModel;
        
        NSArray *arr = videoModel.segs;
        for (NSDictionary *dic in arr) {
            [videoModel setValuesForKeysWithDictionary:dic];
            [self.videoArray addObject:videoModel];
        }

        
        /**
         * 获取vid
         */
        NSArray *subarr1 = [videoModel.url componentsSeparatedByString:@"vid"];
        NSString *s1 = subarr1[1];
        NSArray *subarr2 = [s1 componentsSeparatedByString:@"&"];
        NSString *s2 = subarr2[0];
        s2 = [s2 stringByReplacingOccurrencesOfString:@"=" withString:@""];
        playViewController.url = s2;
        
        [self presentViewController:playViewController animated:YES completion:nil];
        
    }
}


- (IBAction)didClickReturnButton:(id)sender {
    DissMissBack;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
